const app = getApp();   
/**
 * 计算msg总高度
 */
// function calScrollHeight(that, keyHeight) {
//   var query = wx.createSelectorQuery();
//   query.select('.scrollMsg').boundingClientRect(function(rect) {
//   }).exec();
// }

Page({ 
  data: {
    toView:'',
    scrollHeight: '100vh',
    inputBottom: 0, 
    inputVal : '', 
    id:'',
    doctorId:'',
    msgList : [{
        speaker: 'server',
        contentType: 'text',
        content: '欢迎来到智慧中医，有啥问题请咨询，我们第一时间为您解决，祝您身体健康！'
      }
    ],
    cusHeadIcon:'http://img.zhichiwangluo.com/zc_app_default_photo.png',
    windowWidth : wx.getSystemInfoSync().windowWidth,
    windowHeight : wx.getSystemInfoSync().windowHeight,
    keyHeight : 0,
  }, 
  onLoad: function(options) {  
    var loginUserId = wx.getStorageSync('LOGINUSER_ID') || '';
    var _this = this;
    this.setData({
      id: options.id||'',
      doctorId: options.doctorId||'',
    }) 
    if(options.id == undefined || '' == options.id) { 
      wx.request({ 
        url: app.globalData.message_last_url,
        data: {userId:loginUserId,doctorId:_this.data.doctorId},
        method: 'GET', 
        success: function (res) {
          var resultData = res.data;
          if(resultData.errno == 0){  
            _this.setData({id: resultData.data.id}) 
          }
        }
      })   
    } 
    if(_this.data.id==''){return} 
    wx.request({ 
      url: app.globalData.message_mydetail_url,
      data: {id:_this.data.id},
      method: 'GET', 
      success: function (res) {
        var resultData = res.data;
        if(resultData.errno == 0){
          var tmp = _this.data.msgList; 
          for(var i = 0;i< resultData.data.length;i++){
            var row = resultData.data[i];
            var appendRow = {contentType: 'text',content : row.content};  
            if(loginUserId==row.userId){
              appendRow.speaker='coustomer'; 
            }else{
              appendRow.speaker='server';  
            }
            tmp.push(appendRow);
          }
          _this.setData({msgList: tmp}) 
        }
      }
    })  
  },    
  focus: function(e) { 
    var _this = this; 
    this.setData({
      toView: 'msg-' + (_this.data.msgList.length - 1),
      inputBottom:  _this.data.keyHeight + 'px', 
      keyHeight : e.detail.height,
      scrollHeight: (_this.data.windowHeight - _this.data.keyHeight) + 'px',
    })
  },

  //失去聚焦(软键盘消失)
  blur: function(e) {
    var _this = this; 
    this.setData({
      scrollHeight: '100vh',
      inputBottom: 0,
      toView: 'msg-' + (_this.data.msgList.length - 1),
    }) 
  }, 
  sendClick: function(e) { 
    var _this = this;
    var content =  e.detail.value; 
    var loginUserId = wx.getStorageSync('LOGINUSER_ID') || '';
    wx.request({ 
      url: app.globalData.message_add_url,
      data: {id:_this.data.id,userId:loginUserId,doctorId:_this.data.doctorId,content:content},
      method: 'POST', 
      success: function (res) {
        var resultData = res.data;
        if(resultData.errno == 0){
          var tmpMsg = _this.data.msgList;
           tmpMsg.push({
            speaker: 'customer',
            contentType: 'text',
            content: content
          })
          _this.setData({
            msgList: tmpMsg,
            inputVal:'',
            id:resultData.data.id
          }) 
        }
      }
    })  


  },  

})
